package data.nanjing.expo.services;

import data.nanjing.expo.common.ExpoConstants;
import data.nanjing.expo.models.dao.ActivityInfoDao;
import data.nanjing.expo.models.dao.UserFavoriteDao;
import data.nanjing.expo.models.domain.UserFavorite;
import data.nanjing.expo.models.domain.dto.FavoriteShow;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by zjp on 2018/6/28.
 * QQ：34948062
 * github: https://www.github.com/zjp1029
 * web： http://www.zjp1029.cn
 * 用户收藏 favorite_type表示收藏的什么
 */
@Service
public class UserFavoriteService {

    @Autowired
    private UserFavoriteDao userFavoriteDao;

    @Autowired
    private ActivityInfoDao activityInfoDao;

    //活动收藏数添加
    @Transactional
    public void addActivityFavorite(UserFavorite userFavorite) {
        userFavoriteDao.addUserFavorite(userFavorite);
        activityInfoDao.addActivityFavoriteNumber(userFavorite.getFavoriteId());
    }

    //是否收藏,根据用户的id，收藏的类型，收藏的id；如活动:type=activity,活动的id，type从常量取
    public boolean isFavorite(Integer userId, String type, Integer favoriteId) {
        int count = userFavoriteDao.selectFavoriteCountByUserIdAndType(userId, type, favoriteId);
        if (count == 0) {
            return false;
        }else {
            return true;
        }
    }

    //取消收藏
    @Transactional
    public void deleteFavorite(Integer userId, String type, Integer favoriteId) {
        userFavoriteDao.deleteFavoriteByUserIdAndType(userId, type, favoriteId);
        activityInfoDao.increaseActivityFavoriteNumber(favoriteId);
    }

    //查询我的收藏
    public List<FavoriteShow> selectActivityFavoriteByUserId(Integer userId, Integer pageNumber, Integer pageSize, String type) {
        int pageStart = (pageNumber - 1) * pageSize;
        if (type.equals(ExpoConstants.FAVORITE_ACTIVITY)) {
            return userFavoriteDao.selectActivityFavoriteByUserId(userId, pageStart, pageSize);
        }
        return new ArrayList<>();
    }

    //总数
    public Integer selectActivityFavoriteCountByUserId(Integer userId, String type) {
        if (type.equals(ExpoConstants.FAVORITE_ACTIVITY)) {
            return userFavoriteDao.selectActivityFavoriteCountByUserId(userId);
        }
        return 0;
    }

}
